package top.hidoctor.auscultation.feature.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.bean.LableBean;
import top.hidoctor.auscultation.feature.fragment.YangbenFragment;
import top.hidoctor.auscultation.feature.fragment.YishengFragment;
import top.hidoctor.auscultation.net.c.LabelDetailContract;
import top.hidoctor.auscultation.net.p.LabelDetailPresenter;

public class BiaozhuDetailActivity extends BaseActivity implements View.OnClickListener, LabelDetailContract.View {
    private EditText editSearch;
    private FrameLayout frameContent;
    private RadioGroup rgGroup;
    private RadioButton rbYangben, rbYisheng;
    private int[] resId = new int[]{R.id.rb_yangben, R.id.rb_yisheng};
    private ArrayList<Fragment> fragments;
    private int position = 0;
    private FragmentTransaction ft;
    private Fragment CURRENT_FRAGMENT;
    int tab_number = 0;
    private String id = "";
    private LabelDetailPresenter presenter;
    private YangbenFragment yangbenFragment;
    private YishengFragment yishengFragment;
    private LableBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_biaozhu_detail);
        id = getIntent().getStringExtra("id");
        presenter = new LabelDetailPresenter(this, this);
        presenter.getDetail(id, false);
        initView();

    }

    private void initView() {
        final Toolbar toolbar = findViewById(R.id.toolbar);
        editSearch = findViewById(R.id.edit_search);
        toolbar.setNavigationIcon(R.mipmap.ic_back_left);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
                hideSoft(toolbar);
            }
        });

        frameContent = findViewById(R.id.frame_content);
        rgGroup = findViewById(R.id.rg_group);
        rbYangben = findViewById(R.id.rb_yangben);
        rbYisheng = findViewById(R.id.rb_yisheng);
        rbYisheng.setOnClickListener(this);
        rbYangben.setOnClickListener(this);


    }

    private void initEvent() {
        Bundle bundle = new Bundle();
        bundle.putSerializable("info", bean);
        fragments = new ArrayList<>();
        yangbenFragment = new YangbenFragment();
        yangbenFragment.setArguments(bundle);
        fragments.add(0, yangbenFragment);
        yishengFragment = new YishengFragment();
        yishengFragment.setArguments(bundle);
        fragments.add(1, yishengFragment);
        rgGroup.check(resId[position]);
        switchFragment(tab_number);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_yangben:
                tab_number = 0;
                switchFragment(0);
                editSearch.setHint("样本编号");
                break;
            case R.id.rb_yisheng:
                tab_number = 1;
                switchFragment(1);
                editSearch.setHint("医生姓名");
                break;
        }

    }

    /**
     * 选择fragment
     * @param pos
     */
    private void switchFragment(int pos) {
        position = pos;
        rgGroup.check(resId[position]);
        ft = getSupportFragmentManager().beginTransaction();
        if (CURRENT_FRAGMENT != null) {
            ft.hide(CURRENT_FRAGMENT);
        }
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragments.get(pos).getClass().getName());
        if (fragment == null) {
            fragment = fragments.get(pos);
        }
        CURRENT_FRAGMENT = fragment;
        if (!fragment.isAdded()) {
            ft.add(R.id.frame_content, fragment, fragment.getClass().getName());
        } else {
            ft.show(fragment);
        }
        ft.commitAllowingStateLoss();
    }

    /**
     * 获取详情回调
     * @param bean
     */
    @Override
    public void getDetailSuc(LableBean bean) {
        this.bean = bean;
        if (bean != null) {
            initEvent();
        }
    }

    @Override
    public void updateLabelSuc() {

    }
}
